package com.best.onegous.details;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.best.onegous.BaseActivity;
import com.best.onegous.R;
import com.best.onegous.Adapter.WishAdapter;
import com.best.onegous.Refresh.XListView;
import com.best.onegous.pickerview.OptionsPickerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class AddAddress  extends BaseActivity implements OnClickListener{
    private ArrayList<String> mListProvince = new ArrayList<String>();
    private ArrayList<ArrayList<String>> mListCiry = new ArrayList<ArrayList<String>>();
    private ArrayList<ArrayList<ArrayList<String>>> mListArea = new ArrayList<ArrayList<ArrayList<String>>>();
    
    private OptionsPickerView<String> mOpv;
    private JSONObject mJsonObj;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setThem(R.layout.addaddress_activity);
		findViewById(R.id.back).setOnClickListener(this);
		findViewById(R.id.xzdizhi).setOnClickListener(this);
        initJsonData();
        initJsonDatas();
        
        mOpv = new OptionsPickerView<String>(this);

        mOpv.setTitle("选择地区");
        mOpv.setPicker(mListProvince, mListCiry, mListArea, true);
        mOpv.setCyclic(false, false, false);
        mOpv.setSelectOptions(0, 0, 0);
        mOpv.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3) {
                String tx = mListProvince.get(options1) + mListCiry.get(options1).get(option2) + mListArea.get(options1).get(option2).get(options3);
                //mCity.setText(tx);
            }
        });
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			 finish();
			break;
		case R.id.xzdizhi:
			mOpv.show();
			break;
		}
	}
	private void initJsonData() {
		try {
			StringBuffer sb = new StringBuffer();
			InputStream is = getAssets().open("city.json");
			int len = -1;
			byte[] buf = new byte[1024];
			while ((len = is.read(buf)) != -1) {
				sb.append(new String(buf, 0, len, "GBK"));
			}
			is.close();
			mJsonObj = new JSONObject(sb.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private void initJsonDatas(){
		try {
			JSONArray jsonArray = mJsonObj.getJSONArray("citylist");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonP = jsonArray.getJSONObject(i);//  
				String province = jsonP.getString("name");
				
				ArrayList<String> options2Items_01 = new ArrayList<String>();
				ArrayList<ArrayList<String>> options3Items_01 = new ArrayList<ArrayList<String>>();
				JSONArray jsonCs = jsonP.getJSONArray("city");
				for (int j = 0; j < jsonCs.length(); j++) {
					JSONObject jsonC = jsonCs.getJSONObject(j);// 
					String city = jsonC.getString("name");
					options2Items_01.add(city);// 娣诲姞甯傛暟鎹�
					
					ArrayList<String> options3Items_01_01 = new ArrayList<String>();
					JSONArray jsonAs = jsonC.getJSONArray("area");
					for (int k = 0; k < jsonAs.length(); k++) {
						options3Items_01_01.add(jsonAs.getString(k));// 娣诲姞鍖烘暟鎹�
					}
					options3Items_01.add(options3Items_01_01);
				}
				mListProvince.add(province);// 娣诲姞鐪佹暟鎹�
				mListCiry.add(options2Items_01);
				mListArea.add(options3Items_01);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		mJsonObj = null;
	}
}
